<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      row-key="id"
      border
      stripe
      fit
      highlight-current-row>
      <el-table-column prop="proInsName" label="流程实例名称" min-width="200" align="center">
      </el-table-column>
      <el-table-column label="状态" min-width="200" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.processStatus | PROCESS_STATUS }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="userName" label="创建人" min-width="100" align="center">
      </el-table-column>
      <el-table-column prop="currentTaskName" label="当前节点" min-width="100" align="center">
      </el-table-column>
      <el-table-column align="center" label="创建时间" min-width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | moment('YYYY-MM-DD HH:mm:ss') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="80" align="center">
        <template slot-scope="scope">
          <el-button 
            type="primary" 
            size="mini" 
            circle 
            icon="el-icon-view"
            title="查看" 
            @click="handleView(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="fetchData" />
  </div>
</template>
<!--流程监控-->
<script>
import enums from '@/views/workflow/utils/enum'
import { queryAllProcessInstance } from '@/api/workflow/bpmn/runtime'
import Pagination from '@/views/workflow/components/Pagination'
import moment from 'moment'

export default {
  components: { 
    Pagination
  },
  data() {
    return {
      total: 0,
      dateRange: '',
      listQuery: {
        processTypeId: '',
        processStatus: undefined,
        startUserName: undefined,
        proInsName: undefined,
        pageNo: 1,
        pageSize: 10
      },
      list: [],
      listLoading: false
    }
  },
  computed: {
    PROCESS_STATUS_Options() {
      return enums.PROCESS_STATUS
    },
  },
  filters: {
    PROCESS_STATUS(status) {
      return enums.PROCESS_STATUS[status]
    },
  },
  created() {
    this.listQuery.processTypeId = this.$route.query.processTypeId
    this.fetchData()
  },
  methods: {
    handleFilter() {
      this.listQuery.pageNo = 1
      this.fetchData()
    },
    handleReset() {
      this.dateRange = ''
      this.listQuery.startUserName = undefined
      this.listQuery.proInsName = undefined
      this.listQuery.processStatus = undefined
      this.handleFilter()
    },
    fetchData() {
      // this.listLoading = true
      const post = { ...this.listQuery }
      if (this.dateRange && this.dateRange.length === 2) {
        post.startTime = moment(this.dateRange[0]).format('YYYY-MM-DD')
        post.endTime = moment(this.dateRange[1]).format('YYYY-MM-DD')
      }
      queryAllProcessInstance(post).then(response => {
        this.total = response.data.total
        this.list = response.data.records
        this.listLoading = false
      })
    },
    handleView(row) { // 发起流程
      const deploymentId = row.id
      this.$router.push({ 
        path: '/workflow/bpmn/process', 
        query: { 
          procdefId: row.processDefinitionId, 
          deploymentId: row.processDeploymentId,
          taskId: row.currentTaskId,
          processInstanceId: row.proInsId,
          readonly: true,
        }
      })
    }
  }
}
</script>
